#include "MyUI.h"

/*ServerInfo::ServerInfo(wxString conn_name){
	mysql = _InitMySQL(NULL);
}*/

MyUI::MyUI(wxFrame* parent,ServerInfo* serverInfo){
	this->parent = parent;
	this->serverInfo = serverInfo;
	
	m_mgr.SetManagedWindow(parent);
	
	CreateMenuBar();
	
	CreateToolBar();
	
    treeview = new wxTreeCtrl( parent, ID_TREECTRL, wxDefaultPosition, wxSize(240, 100), wxTR_HAS_BUTTONS | wxTR_HIDE_ROOT | wxTR_LINES_AT_ROOT );
	
	editNotebook = new EditNotebook(parent,ID_EDIT_NOTEBOOK,wxDefaultPosition,wxSize(-1,600),serverInfo->conn_name);
	editNotebook->AddEdit(serverInfo->mysql,serverInfo->conn_name);
	
    //_AddDbListToTreeCtrl(serverInfo->mysql,treeview,editNotebook,serverInfo->conn_name,currentDb);
	
	
	
	result = new ResultNotebook(parent,wxID_ANY,wxDefaultPosition,wxDefaultSize);
	
	log = new Edit(parent,wxID_ANY,wxDefaultPosition,wxSize(-1,120),wxSUNKEN_BORDER|wxVSCROLL,0,false);
	
	m_mgr.AddPane(log,wxAuiPaneInfo().Name(wxT("Log")).Caption(wxT("Query Log")).Bottom().Layer(1).Position(1).CloseButton(false).MaximizeButton(false));
	
    m_mgr.AddPane(treeview, wxAuiPaneInfo().
                  Name(wxT("treeview")).Caption(wxT("Database Info")).
                  Left().Layer(1).Position(1).
                  CloseButton(false).MaximizeButton(false));
	
    m_mgr.AddPane(editNotebook, wxAuiPaneInfo().
                  Name(wxT("editor")).CenterPane().Top());
	
    m_mgr.AddPane(result, wxAuiPaneInfo().
                  Name(wxT("output")).CenterPane());
	
	m_mgr.Update();
	
	
	CreateTaskBar();
	CreateStatusBar();
	
	//sqlThread = new UIThread(serverInfo->mysql,result,s_bar,currentDb,log,0,array);

}

void MyUI::CreateMenuBar(){
	// File
    wxMenuBar* menuBar = new wxMenuBar;
    wxMenu* fileMenu = new wxMenu;
	fileMenu->Append(ID_MENUITEM_FILE_NEW,_("New\tCtrl+N"));
	fileMenu->Append(ID_MENUITEM_FILE_OPEN,_("Open\tCtrl+O"));
	fileMenu->Append(ID_MENUITEM_FILE_SAVE,_("Save\tCtrl+S"));
	fileMenu->AppendSeparator();
	fileMenu->Append(ID_MENUITEM_FILE_NEW_CONNECTION,_("New Connection\tAlt+N"));
	fileMenu->AppendSeparator();
    fileMenu->Append(ID_MENUITEM_FILE_EXIT, _("Exit\tAlt+Q"));
	menuBar->Append(fileMenu, _("File"));
	
	// View
	wxMenu* viewMenu = new wxMenu;
	viewMenu->Append(ID_MENUITEM_VIEW_AUTOCOMP,wxT("AutoComp\tF8"),wxT(""),wxITEM_CHECK);
	viewMenu->Append(ID_MENUITEM_VIEW_LOG,wxT("Query Log\tCtrl+2"),wxT(""),wxITEM_CHECK );
	viewMenu->Append(ID_MENUITEM_VIEW_DB,wxT("Database Info\tCtrl+1"),wxT(""),wxITEM_CHECK );
	menuBar->Append(viewMenu,wxT("View"));
    parent->SetMenuBar(menuBar);
	menuBar->Check(ID_MENUITEM_VIEW_DB,true);
	menuBar->Check(ID_MENUITEM_VIEW_LOG,true);
	//menuBar->Check(ID_MENUITEM_VIEW_AUTOCOMP,true);
	
	// Build
	wxMenu* buildMenu = new wxMenu;
	buildMenu->Append(ID_MENUITEM_BUILD_RUN_ALL,wxT("Run All\tF5"));
	//buildMenu->Append(ID_MENUITEM_BUILD_RUN_SELECTED,wxT("Run Selected\tF4"));
	buildMenu->Append(ID_MENUITEM_BUILD_RUN_LINE,wxT("Run Current Line\tF6"));
	buildMenu->Append(ID_MENUITEM_BUILD_RUN_TEXT,wxT("Run Current Line In Text\tF7"));
	
	menuBar->Append(buildMenu,wxT("Build"));
    parent->SetMenuBar(menuBar);
	
	//Tools
	wxMenu* toolsMenu = new wxMenu;
	toolsMenu->Append(ID_MENUITEM_TOOLS_EXPORT,wxT("Export Result...\tCtrl+E"));
	menuBar->Append(toolsMenu,wxT("Tools"));
    parent->SetMenuBar(menuBar);
	
	// Help
	wxMenu* helpMenu = new wxMenu;
    helpMenu->Append(ID_MENUITEM_HELP_ABOUT, _("About\tAlt+A"));
	menuBar->Append(helpMenu,wxT("Help"));
	
}


void MyUI::CreateToolBar(){
	wxToolBar* t_bar = new wxToolBar(parent, ID_TOOLBAR, wxDefaultPosition, wxDefaultSize,
								  wxTB_FLAT | wxTB_NODIVIDER);
    t_bar->SetToolBitmapSize(wxSize(32, 32));
	wxBitmap reconnect(reconnect_xpm);
	t_bar->AddTool(ID_TB_NEW_CONNECT,_("New Connection"),reconnect,reconnect, wxITEM_NORMAL, _("New Connection"), wxEmptyString);
	
    wxBitmap itemtool6Bitmap(new_xpm);
    wxBitmap itemtool6BitmapDisabled;
    t_bar->AddTool(ID_TB_NEW, _("New"), itemtool6Bitmap, itemtool6BitmapDisabled, wxITEM_NORMAL, _("New\nCtrl+N"), wxEmptyString);
    wxBitmap itemtool7Bitmap(open_xpm);
    wxBitmap itemtool7BitmapDisabled;
    t_bar->AddTool(ID_TB_OPEN, _("Open"), itemtool7Bitmap, itemtool7BitmapDisabled, wxITEM_NORMAL, _("Open\nCtrl+O"), wxEmptyString);
    wxBitmap itemtool8Bitmap(save_xpm);
    wxBitmap itemtool8BitmapDisabled;
    t_bar->AddTool(ID_TB_SAVE, _("Save"), itemtool8Bitmap, itemtool8BitmapDisabled, wxITEM_NORMAL, _("Save\nCtrl+S"), wxEmptyString);
    t_bar->AddSeparator();
	
    wxBitmap itemtool9Bitmap(run_xpm);
    wxBitmap itemtool9BitmapDisabled;
    t_bar->AddTool(ID_TB_RUN, _("Run"), itemtool9Bitmap, itemtool9BitmapDisabled, wxITEM_NORMAL, _("Run\n(F5)"), wxEmptyString);
	
    wxBitmap itemtool10Bitmap(run_line_xpm);
    wxBitmap itemtool10BitmapDisabled;
    t_bar->AddTool(ID_TB_RUN_LINE, _("Run Current Line"), itemtool10Bitmap, itemtool10BitmapDisabled, wxITEM_NORMAL, _("Run Current Line\n(F6)"), wxEmptyString);
	
	wxBitmap itemtool11Bitmap(run_text_xpm);
    wxBitmap itemtool11BitmapDisabled;
    t_bar->AddTool(ID_TB_RUN_TEXT, _("Run In Text"), itemtool11Bitmap, itemtool11BitmapDisabled, wxITEM_NORMAL, _("Run In Text\n(F7)"), wxEmptyString);

	wxBitmap itemtool12Bitmap(stop_xpm);
    wxBitmap itemtool12BitmapDisabled;
    t_bar->AddTool(ID_TB_STOP, _("Stop"), itemtool12Bitmap, itemtool12BitmapDisabled, wxITEM_NORMAL, _("Stop"), wxEmptyString);

    
	t_bar->AddSeparator();
	

	currentFontEncoding = new wxChoice ( t_bar, ID_TB_FONT_ENCODING );
	currentFontEncoding->SetToolTip(wxT("Font Encoding"));
	currentFontEncoding->Append(wxT("System Default"));
	currentFontEncoding->Append(wxT("GBK"));
	currentFontEncoding->Append(wxT("UTF-8"));
	currentFontEncoding->Select(0);
	
	t_bar->AddControl(currentFontEncoding);
    t_bar->AddSeparator();
	currentDb = new wxChoice( t_bar, ID_TB_CURRENT_DB);
	t_bar->AddControl(currentDb);
    t_bar->Realize();
    
	parent->SetToolBar(t_bar);	
}

void MyUI::CreateTaskBar(){
	taskBarIcon = new MyTaskBarIcon(parent);
#ifdef __WXMAC__
	taskBarIcon->SetIcon(wxIcon(mysql_mac_xpm),VERSION);
#else
	taskBarIcon->SetIcon(wxIcon(mysql_xpm),VERSION);
#endif	
}

void MyUI::CreateStatusBar(){
	s_bar = new wxStatusBar( parent, ID_STATUSBAR,  wxST_SIZEGRIP | wxALIGN_RIGHT  );
	const int widths[] = {-1,100,100,100,180,180,150};
    s_bar->SetFieldsCount(WXSIZEOF(widths));
	s_bar->SetStatusWidths(WXSIZEOF(widths),widths);
	s_bar->SetStatusText(wxString(serverInfo->mysql->host,*charset),1);
	s_bar->SetStatusText(wxString(serverInfo->mysql->user,*charset),2);
	s_bar->SetStatusText(wxString(serverInfo->mysql->db,*charset),3);
    parent->SetStatusBar(s_bar);
}

MyUI::~MyUI(){
	m_mgr.UnInit();
	delete taskBarIcon;
}